package com.hxx.widget

import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Rect
import android.text.TextPaint
import android.util.Log
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import androidx.annotation.LayoutRes
import androidx.annotation.NonNull
import androidx.appcompat.widget.AppCompatEditText

/**
 *  填充布局
 */
inline fun ViewGroup.loadFromLayoutId(@LayoutRes layoutId: Int): View {
    return LayoutInflater.from(context).inflate(layoutId, this)
}

/**
 *  列表视图填充子布局
 */
fun ViewGroup.inflateFromLayoutId(@LayoutRes layoutId: Int): View {
    return LayoutInflater.from(context).inflate(layoutId, this, false)
}

fun Any.logi(@NonNull message: Any) {
    Log.i(javaClass.simpleName + "日志", "$message")
}

fun Any.loge(@NonNull message: Any) {
    Log.e(javaClass.simpleName + "日志 错误警告", "$message")
}

fun Int.getMode(): Int {
    return View.MeasureSpec.getMode(this)
}

fun Int.getSize(): Int {
    return View.MeasureSpec.getSize(this)
}

interface SoftWindowStateListener {
    fun onSoftWindowOpen(visibleRect: Rect, localRect: Rect)
    fun onSoftWindowClose(visibleRect: Rect, localRect: Rect)
}

fun View.createBottomShowAnim(width: Int): ValueAnimator {
    return ValueAnimator.ofInt(-width, 0).apply {
        duration = 150
        addUpdateListener {
            val position = it.animatedValue as Int
            val view = this@createBottomShowAnim
            view.translationX = position.toFloat()
        }
    }
}

fun View.show() {
    visibility = View.VISIBLE
}

fun View.hide() {
    visibility = View.INVISIBLE
}

fun View.gone() {
    visibility = View.GONE
}

fun View.softInputWindowListener(listener: SoftWindowStateListener) {
    var currentSoftInputWindowShow = false
    viewTreeObserver.addOnGlobalLayoutListener {
        val visibleRect = Rect()
        rootView.getWindowVisibleDisplayFrame(visibleRect)
        val localRect = Rect()
        rootView.getLocalVisibleRect(localRect)
        val isSoftInputWindowShow = localRect.bottom - visibleRect.bottom > 200
        currentSoftInputWindowShow = if (isSoftInputWindowShow) {
            if (currentSoftInputWindowShow != isSoftInputWindowShow) {
                listener.onSoftWindowOpen(visibleRect, localRect)
            }
            true
        } else {
            if (currentSoftInputWindowShow) {
                listener.onSoftWindowClose(visibleRect, localRect)
            }
            false
        }
    }
}

fun AppCompatEditText.hideSoftInputWindow() {
    val ims = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    ims.hideSoftInputFromWindow(windowToken, 0)
}

fun AppCompatEditText.showSoftInputWindow() {
    val ims = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    ims.showSoftInput(this,0)
}

@SuppressLint("ClickableViewAccessibility")
fun AppCompatEditText.scrollError(){
    setOnTouchListener { _, event -> //canScrollVertically()方法为判断指定方向上是否可以滚动,参数为正数或负数,负数检查向上是否可以滚动,正数为检查向下是否可以滚动
        if (canScrollVertically(1) ||canScrollVertically(-1)) {
            parent.requestDisallowInterceptTouchEvent(true) //requestDisallowInterceptTouchEvent();要求父类布局不在拦截触摸事件
            if (event.action == MotionEvent.ACTION_UP) { //判断是否松开
                parent.requestDisallowInterceptTouchEvent(false) //requestDisallowInterceptTouchEvent();让父类布局继续拦截触摸事件
            }
        }
        false
    }
}

fun Int.printInfo() {
    when (getMode()) {
        View.MeasureSpec.EXACTLY -> {
            logi("$this,EXACTLY,${getSize()}")
        }
        View.MeasureSpec.UNSPECIFIED -> {
            logi("$this,UNSPECIFIED,${getSize()}")
        }
        View.MeasureSpec.AT_MOST -> {
            logi("$this,AT_MOST,${getSize()}")
        }
    }
}

/**
 * 获取绘制文字的中心点与基准线的偏移距离
 */
fun TextPaint.getCenterYOffset(): Int {
    return fontMetricsInt.run {
        val h = bottom - top
        val centerY = h / 2
        centerY - bottom
    }
}